package com.ruoyi.pm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pm.domain.ExcPortConfig;
import com.ruoyi.pm.service.IExcPortConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口参数配置Controller
 *
 * @author ruoyi
 * @date 2022-01-20
 */
@RestController
@RequestMapping("/pm/excPortConfig")
public class ExcPortConfigController extends BaseController {
    @Autowired
    private IExcPortConfigService excPortConfigService;

    /**
     * 查询接口参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExcPortConfig excPortConfig) {
        startPage();
        List<ExcPortConfig> list;

        list = excPortConfigService.selectExcPortConfigList(excPortConfig);

        return getDataTable(list);
    }

    /**
     * 导出接口参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:export')")
    @Log(title = "接口参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExcPortConfig excPortConfig) {
        List<ExcPortConfig> list = excPortConfigService.selectExcPortConfigList(excPortConfig);
        ExcelUtil<ExcPortConfig> util = new ExcelUtil<ExcPortConfig>(ExcPortConfig.class);
        util.exportExcel(response, list, "接口参数配置数据");
    }

    /**
     * 获取接口参数配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(excPortConfigService.selectExcPortConfigById(id));
    }

    /**
     * 新增接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:add')")
    @Log(title = "接口参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExcPortConfig excPortConfig) {
        return toAjax(excPortConfigService.insertExcPortConfig(excPortConfig));
    }

    /**
     * 新增接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:add')")
    @Log(title = "接口参数配置", businessType = BusinessType.INSERT)
    @PostMapping("/addList/{portId}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addList(@RequestBody List<ExcPortConfig> excPortConfigList,@PathVariable("portId") Long portId) {

        return toAjax(excPortConfigService.insertExcPortConfigList(excPortConfigList,portId));

    }

    /**
     * 修改接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:edit')")
    @Log(title = "接口参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExcPortConfig excPortConfig) {
        return toAjax(excPortConfigService.updateExcPortConfig(excPortConfig));
    }

    /**
     * 修改接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:edit')")
    @Log(title = "接口参数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/editList")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editList(@RequestBody List<ExcPortConfig> excPortConfigList) {
        for (ExcPortConfig excPortConfig : excPortConfigList) {
            excPortConfigService.updateExcPortConfig(excPortConfig);
        }
        return AjaxResult.success();
    }

    /**
     * 修改排序
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:edit')")
    @Log(title = "修改排序", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOrderNum")
    public AjaxResult updateOrderNum(@RequestBody List<ExcPortConfig> excPortConfigList) {
        return toAjax(excPortConfigService.updateOrderNum(excPortConfigList));
    }


    /**
     * 删除接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:remove')")
    @Log(title = "接口参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(excPortConfigService.deleteExcPortConfigByIds(ids));
    }

    /**
     * 删除接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:remove')")
    @Log(title = "接口参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{portId}/{type}")
    public AjaxResult delByPortIdAndType(@PathVariable("portId") Long portId, @PathVariable("type") String type) {
        return toAjax(excPortConfigService.delByPortIdAndType(portId, type));
    }

    /**
     * 报文导入
     *
     * @param excPortConfig 接口参数配置
     */
    /**
     * 修改接口参数配置
     */
    @PreAuthorize("@ss.hasPermi('pm:excPortConfig:add')")
    @Log(title = "报文导入", businessType = BusinessType.INSERT)
    @PostMapping("/saveJsonData/{saveType}")
    public AjaxResult saveJsonData(@PathVariable("saveType") String saveType,@RequestBody ExcPortConfig excPortConfig) {
        excPortConfigService.saveJsonData(excPortConfig,saveType);
        return AjaxResult.success();
    }
}
